package per.hnvcam.searchext.data.dao.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import per.hnvcam.searchext.data.dao.WordDAO;
import per.hnvcam.searchext.data.model.Word;

import java.util.List;

@Service("WordDAO")
@Transactional
public class WordDAOImpl extends BaseDAOImpl implements WordDAO {
   @SuppressWarnings("unchecked")
   @Override
   public List<Word> listWords() {
      return getHibernateTemplate().find("from Word");
   }

   @Override
   public Word findById(long id) {
      return getHibernateTemplate().get(Word.class, id);
   }

   @Override
   public Word findByLabel(String label) {
      @SuppressWarnings("rawtypes")
      List result = getHibernateTemplate().find("from Word w where w.label = ?", label.toLowerCase());
      if (result != null && result.size() > 0) {
         return (Word) result.get(0);
      }
      return null;
   }

   @Override
   public void save(Word word) {
      getHibernateTemplate().save(word);
   }

   @Override
   public void update(Word word) {
      getHibernateTemplate().update(word);
   }
}
